#include <string>
#include <iostream>
#include <memory>
#include <grpcpp/grpcpp.h>
#include "demo.grpc.pb.h"

class GrpcClient {
public:
	GrpcClient(std::shared_ptr<grpc::Channel> channel)
		:_stub(hello::Greeter::NewStub(channel)) {

	}
	std::string SayHello(std::string message) {
		grpc::ClientContext context;
		hello::HelloReply reply;
		hello::HelloRequest request;
		request.set_message(message);

		grpc::Status status = _stub->SayHello(&context, request, &reply);

		if (status.ok()) {
			return reply.message();
		}
		else {
			return "fail SayHello " + status.error_message();
		}
	}
private:
	std::unique_ptr<hello::Greeter::Stub> _stub;
};

int main(int argc, char** argv) {
	auto channel = grpc::CreateChannel("127.0.0.1:40000", grpc::InsecureChannelCredentials());
	GrpcClient client(channel);

	std::string result = client.SayHello("hello word");
	std::cout << "result is " << result << std::endl;
	return 0;
}